<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //保存
        setCookie = (cname, cvalue, exdays) => {
            var day = new Date();
            day.setTime(day.getTime + (exdays * 24 * 60 * 60 / 1000))
            var expires = 'expires=' + day.toGMTString();
            document.cookie = cname + '=' + cvalue + ';' + expires;
        }
         setCookie("name", 'jack', 2)
         setCookie("password", '6586720', 2)
        //读取
        getCookie = (cname) => {
            var name = cname + '='
            var ca = document.cookie.split(';')
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i].trim()
                if (c.indexOf(name) == 0) {
                    return c.substring(name.length, c.length)
                }
            }
            return ''
        }
        console.log(getCookie("name"))
        // 检测
        checkCookie = () => {
            var Vname = getCookie("-name");
            if (Vname != "") {
                alert("Hellow :" + Vname);
            }
            else {
                Vname = prompt("请输入你的名字:", "");
                if (Vname != "" && Vname != null) {
                    setCookie("Vname", Vname, 2);
                }
            }
        }
        checkCookie()
    </script>
</body>

</html>